package org.jpro.common.jpa.specification;

import java.io.Serializable;

import javax.persistence.criteria.From;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

/**
 * <P>功能描述 : </P>
 * <P>Method Name : AbstractSpecification </P>
 * <P>Author : 王泽浩 </P>
 * <P>E-Mail : 1028625100@qq.com </P>
 * <P>Date : 2017-03-14 15:44:41</P>
 * @param <T> the type parameter
 * @since 1.0
 */
public abstract class AbstractSpecification<T> implements Specification<T>, Serializable {

    /**
     * Gets property.
     * @param property the property
     * @return the property
     */
    public String getProperty(String property) {
        if (property.contains(".")) {
            return StringUtils.split(property, ".")[1];
        }
        return property;
    }

    /**
     * Gets root.
     * @param property the property
     * @param root the root
     * @return the root
     */
    public From getRoot(String property, Root<T> root) {
        if (property.contains(".")) {
            String joinProperty = StringUtils.split(property, ".")[0];
            return root.join(joinProperty, JoinType.LEFT);
        }
        return root;
    }

}
